package com.example.umengdemo.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.os.Build
import android.text.TextUtils
import android.util.Log
import java.net.Inet6Address
import java.net.InetAddress
import java.net.NetworkInterface
import java.net.SocketException
import java.util.*

/**
 * @auth: njb
 * @date: 2021/7/15 14:03
 * @desc: 获取版本及手机信息工具类
 */
object CommonUtils {
    /**
     * 获取版本号(内部识别号) = 101
     *
     * @param context
     * @return
     */
    fun getVersionCode(context: Context): Int {
        return try {
            val pi = context.packageManager.getPackageInfo(context.packageName, 0)
            pi.versionCode
        } catch (e: PackageManager.NameNotFoundException) {
            // TODO Auto-generated catch block
            e.printStackTrace()
            0
        }
    }

    /**
     * 获取版本号 = "1.0.1";
     *
     * @param context
     * @return
     */
    fun getVersionName(context: Context): String {
        return try {
            val pi = context.packageManager.getPackageInfo(context.packageName, 0)
            pi.versionName
        } catch (e: PackageManager.NameNotFoundException) {
            // TODO Auto-generated catch block
            e.printStackTrace()
            "未知版本"
        }
    }



    /**
     * 根据Wifi信息获取本地Mac
     *
     * @param context
     * @return
     */
    @SuppressLint("HardwareIds")
    fun getLocalMacAddressFromWifiInfo(context: Context): String? {
        var info: WifiInfo? = null
        try {
            val wifi = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
            info = wifi.connectionInfo
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return info?.macAddress
    }// skip ipv6

    /**
     * 获取ip地址
     *
     * @return
     */
    val hostIP: String
        get() {
            var hostIp = ""
            try {
                val nis: Enumeration<*> = NetworkInterface.getNetworkInterfaces()
                var ia: InetAddress? = null
                while (nis.hasMoreElements()) {
                    val ni = nis.nextElement() as NetworkInterface
                    val ias = ni.inetAddresses
                    while (ias.hasMoreElements()) {
                        ia = ias.nextElement()
                        if (ia is Inet6Address) {
                            continue  // skip ipv6
                        }
                        val ip = ia.hostAddress
                        if ("127.0.0.1" != ip) {
                            hostIp = ia.hostAddress
                            break
                        }
                    }
                }
            } catch (e: SocketException) {
                Log.e("e", "SocketException")
                e.printStackTrace()
            }
            return hostIp
        }

    /**
     * 获取手机型号
     *
     * @return
     */
    val phoneModel: String
        get() {
            val model = Build.MODEL
            return if (TextUtils.isEmpty(model)) {
                ""
            } else model
        }

    /**
     * 获取手机系统版本号
     *
     * @return
     */
    val versionRelease: String
        get() {
            val version = Build.VERSION.RELEASE
            return if (TextUtils.isEmpty(version)) {
                ""
            } else version
        }

    /**
     * 是否有访问所有文件的权限
     *
     * @param activity
     * @return
     */
    fun isRequestAllFileManager(activity: Activity?): Boolean {
        /*   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
            //手机版本为Android11且没有申请权限跳转新页面申请权限，有权限则去做相应工作
            Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
            intent.setData(Uri.parse("package:" + AppUtils.getPackageName(activity)));
            activity.startActivity(intent);
            return false;
        }*/
        return true
    }

    /**
     * 检测是否安装支付宝
     * @param context
     * @return
     */
    fun isAliPayInstalled(context: Context): Boolean {
        val uri = Uri.parse("alipays://platformapi/startApp")
        val intent = Intent(Intent.ACTION_VIEW, uri)
        val componentName = intent.resolveActivity(context.packageManager)
        return componentName != null
    }

    /**
     * 检测是否安装微信
     * @param context
     * @return
     */
    fun isWeChatInstalled(context: Context): Boolean {
        val packageManager = context.packageManager // 获取packagemanager
        val packageInfo = packageManager.getInstalledPackages(0) // 获取所有已安装程序的包信息
        for (i in packageInfo.indices) {
            val pn = packageInfo[i].packageName
            if (pn == "com.tencent.mm") {
                return true
            }
        }
        return false
    }
}